#include "bsp_alsa.h"
#include <iostream>
#include <cstdint>
#include "fp_device.h"
#include "bsp_alsa.h"

using namespace std;

void print_usage(const char *program_name);
/**
 * @brief 主程序入口，解析参数并执行录音
 */
int main(int argc, char *argv[])
{
    std::string device = AlsaRecorder::DEFAULT_DEVICE;
    unsigned int sample_rate = AlsaRecorder::DEFAULT_SAMPLE_RATE;
    int channels = AlsaRecorder::DEFAULT_CHANNELS;
    snd_pcm_format_t format = AlsaRecorder::DEFAULT_FORMAT;
    int duration = AlsaRecorder::DEFAULT_DURATION;
    std::string filename = AlsaRecorder::DEFAULT_FILENAME;

    int opt;
    while ((opt = getopt(argc, argv, "d:r:c:f:t:o:")) != -1)
    {
        switch (opt)
        {
        case 'd':
            device = optarg;
            break;
        case 'r':
            sample_rate = std::atoi(optarg);
            break;
        case 'c':
            channels = std::atoi(optarg);
            break;
        case 'f':
            format = AlsaRecorder::parse_format(optarg);
            break;
        case 't':
            duration = std::atoi(optarg);
            break;
        case 'o':
            filename = optarg;
            break;
        default:
            print_usage(argv[0]);
            return EXIT_FAILURE;
        }
    }

    std::cout << "Recording with parameters:\n";
    std::cout << "  Device: " << device << "\n";
    std::cout << "  Sample Rate: " << sample_rate << " Hz\n";
    std::cout << "  Channels: " << channels << "\n";
    std::cout << "  Format: " << snd_pcm_format_name(format) << "\n";
    std::cout << "  Duration: " << duration << " seconds\n";
    std::cout << "  Output File: " << filename << "\n";

    AlsaRecorder recorder(device, sample_rate, channels, format, duration, filename);
    bool result = recorder.record();
    return result ? EXIT_SUCCESS : EXIT_FAILURE;
}
